<?php
// $Id$

/**
 * Controller_Admin_Groups 控制器
 */
class Controller_Admin_Groups extends Controller_Abstract
{

    function actionIndex()
    {
        //这里是搜索字段和操作符，like就是模糊搜索了，=就是严格等于，很好理解
        $wheres = array('name'=>'like',);
        $this->_getJQGridData(Role::find(), $wheres, 'call_columns, call_departments');
    }

    function actionViewCallColumns() {
        $group = Role::find('role_id = ?', $this->_context->role_id)->one()->query('call_columns');
        $this->_view['group'] = $group;
        //        Qdebug::dump($group);
    }

    function actionViewCallDepartments() {
        $group = Role::find('role_id = ?', $this->_context->role_id)->one()->query('call_columns');
        $this->_view['group'] = $group;
        //        Qdebug::dump($group);
    }

    function actionRemove() {
        $user = Helper_RBAC::get('user');
        $roles = Helper_RBAC::roles('user');
        $idset = FUNC::getIdset($this->_context->idset);
        foreach($idset as $id) {
            $record = Role::find('role_id = ?', $id)->one()->query();
            if(in_array('ADMIN', $roles)) {
                Role::meta()->destroyWhere('role_id = ?', $id);
            }
        }
    }

    function actionEdit() {
        $condi[] = new QDB_Cond(
            'role_id = ?', $this->_context->group_id
        );
        $group = Role::find($condi)->one()->query('call_columns');
        $this->_view['group'] = $group;
    }

    function actionSave() {

        $condi[] = new QDB_Cond(
            'name like ?', '%'.$this->_context->name.'%'
        );
        $where[] = new QDB_Cond(
            'role_id = ?', $this->_context->group_id
        );

        $group = Role::find($condi)->orWhere($where)->one()->query();
        if($group->id()) {
            //            echo 'bb';
            //            Qdebug::dump($_POST);
            $column = Column::find('col_id = ?', 0)->all()->query();
            //        Qdebug::dump($column);
            $group->call_columns = $column;
        }
        else {
            //            echo 'aa';
        }
        $group->name = $this->_context->name;
        $column_ids = FUNC::getIdset(FUNC::getV('call_columns'));
        //        Qdebug::dump($column_ids);

        if($column_ids) {
            //            echo 'kkkk';
            foreach($column_ids as $key=>$col_id) {
                $column = Column::find('col_id = ?', $col_id)->one()->query();
                $group->call_columns[] = $column;
                //                $group->call_columns[] = new Column(
                //                    array('col_id'=>$col_id, 'name'=>)
                //                );
            }
        }
        else {
            //            echo 'ccc';
            $column = Column::find('col_id = ?', 0)->all()->query();
            //        Qdebug::dump($column);
            $group->call_columns = $column;
        }

        $department_ids = $this->_context->depart_id;
        if($department_ids) {
//            dump($department_ids);
            $department = Department::find('depart_id = ?', 0)->all()->query();
            $group->call_departments = $department;
            foreach($department_ids as $key=>$depart_id) {
                if(!$depart_id) continue;
                $department = Department::find('depart_id = ?', $depart_id)->one()->query();
                $group->call_departments[] = $department;
            }
        }
        else {
            $department = Department::find('depart_id = ?', 0)->all()->query();
            $group->call_departments = $department;
        }

        //        Qdebug::dump($group);
        //        exit();
        $group->save();
        //        echo $group->isNewRecord;
        //exit();
        if(!$this->_context->group_id) return $this->_redirectMessage('网站提示','数据已保存', url(''));
        $referer = $this->_context->referer() ? $this->_context->referer() : url('');
        return $this->_redirectMessage('网站提示','数据已保存', $referer);
    }

}


